package com.icc.socket;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    private EditText edit;
    private EditText address;
    private EditText port;
    private OutputStream outputStream;
    private InputStream inputStream;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text);
        edit = findViewById(R.id.edit);
        address = findViewById(R.id.address);
        port = findViewById(R.id.port);


    }

    //发送按钮
    public void send(View view) {
        String s = edit.getText().toString();
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    if (TextUtils.isEmpty(s)) {

                        outputStream.write("物联网 1901 张成 2021520489".getBytes());

                    } else {
                        outputStream.write(s.getBytes());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }

    public void link(View view) {



        check
        String str_port = port.getText().toString();
        String str_address = address.getText().toString();
        if (TextUtils.isEmpty(str_address)) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        ServerSocket socket = new ServerSocket(Integer.parseInt(str_port));

                        Socket accept = socket.accept();
                        outputStream = accept.getOutputStream();
                        inputStream = accept.getInputStream();
                        System.out.println("socket");
                        while (true) {
                            byte[] bytes = new byte[1024];
                            int len = inputStream.read(bytes);
                            String res = new String(bytes);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    textView.setText(res);
                                }
                            });
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();

        } else {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Socket socket = new Socket(str_address, Integer.parseInt(str_port));
                        outputStream = socket.getOutputStream();
                        inputStream = socket.getInputStream();
                        while (true) {
                            byte[] bytes = new byte[1024];
                            int len = inputStream.read(bytes);
                            String res = new String(bytes);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    textView.append(res + "\n");
                                }
                            });
                        }

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    }
}